<html><head><title>{PAGE_TITLE} {VERSION}{TESTNET}</title>
<script type="text/javascript" src="webui/gocoin.js"></script>
<link rel="stylesheet" href="webui/gocoin.css" type="text/css">
<link rel="shortcut icon" href="/webui/favicon.ico" type="image/x-icon">
<script>
/*_SESSION_ID_*/
/*_AVERAGE_FEE_SPB_*/
/*_SERVER_MODE_*/
/*_WALLET_ON_*/
/*_CHAIN_IN_SYNC_*/
var doc_title = document.title
var testnet = (doc_title.indexOf(" Testnet") != -1)

var new_block_audio_file = new Audio('webui/new_block.mp3')
var back_online_audio_file = new Audio('webui/back_online.mp3')

var last_blocks_left = -1

var webuimenu = [
	["/", "Home"],
	["/wal", "Wallet"],
	["/snd", "MakeTx"],
	["/net", "Network"],
	["/txs", "Transactions"],
	["/blocks", "Blocks"],
	["/miners", "Miners"],
	["/counts", "Counters"]
]

const BN_OFFLINE = "XXX"

function beep_clicked() {
	beep_new_block.checked = !beep_new_block.checked
	localStorage.setItem("new_block_beep", beep_new_block.checked?"true":"false")
}
</script>
</head>
<body>
<table align="center" width="1020" cellpadding="0" cellspacing="0"><tr><td>
	<table width="100%" border="0" cellspacing="0"><tr>
		<colgroup>
			<col>  <!-- top menu -->
			<col width="90"> <!-- block height -->
			<col width="60"> <!-- beep -->
			<col width="70"> <!-- status icons -->
			<col width="80"> <!-- PushTX -->
			<col> <!-- right menu -->
		</colgroup>

		<td id="topmenu"></td>
		<td align="center" class="nw bold" id="blno">...</td>
		<td align="center" class"min" title="Beep on each new block" class="hand" onclick="beep_clicked()">
			<input type="checkbox" id="beep_new_block" class="hand"><img src="webui/music.png">
		</td>
		<td align="center" align="center">
			<img id="chainsync_icon" src="webui/chainsync.png" class="hand" style="display:none" onclick="set_tblock_hash()">
		</td>
		<td align="center">
			<a href="javascript:pushtx()" class="topmenu" title="Load Raw TX">PushTx</a>
		<td align="right" id="rightmenu">
			<a id="helpmenulink" href="{HELPURL}" class="topmenu">Help</a>
		</td>
	</tr></table>
<hr style="margin:2px">
<script>

function set_tblock_hash() {
	var hash = prompt("To speed up chain sync, enter hash of the highest block you trust")
	if (hash!=null) {
		var aj = ajax()
		aj.onload = function() {
			location.reload()
		}
		aj.open("GET","cfg?sid="+sid+"&trusthash="+hash, true)
		aj.send(null)
	}
}

function apply_wallet_on() {
	if (wallet_on) {
		topmenu.getElementsByTagName('a')[1].classList.remove('dimenu')
		topmenu.getElementsByTagName('a')[2].classList.remove('dimenu')
	} else {
		topmenu.getElementsByTagName('a')[1].classList.add('dimenu')
	topmenu.getElementsByTagName('a')[2].classList.add('dimenu')
	}
}

for (var i=0; i < webuimenu.length; i++) {
	var a = document.createElement('a')
	a.href = webuimenu[i][0]
	a.text = webuimenu[i][1]
	a.className = 'topmenu'
	if (document.location.pathname==webuimenu[i][0]) {
		a.classList.add('menuat')
	}
	if (i < webuimenu.length-1) {
		topmenu.appendChild(a)
	} else {
		rightmenu.insertBefore(a, helpmenulink)
	}

	if (i < webuimenu.length-2) {
		var sp = document.createElement('span')
		sp.innerHTML = ' '
		topmenu.appendChild(sp)
	}
}
apply_wallet_on()
if (document.location.pathname=='/help') helpmenulink.classList.add('menuat')

var time_now /*_TIME_NOW_*/
function refreshblock() {
	var aj = ajax()
	aj.onerror=function() {
		blno.innerText = BN_OFFLINE
		setTimeout(refreshblock, 10000)
	}
	aj.onload=function() {
		var new_block = false
		try {
			var stat = JSON.parse(aj.responseText)

			var blocks_left = stat.LastHeaderHeight - stat.Height
			if (blocks_left != last_blocks_left) {
				chainsync_icon.title = blocks_left + " blocks left"
			}
			if (chain_in_sync != stat.BlockChainSynchronized) {
				chain_in_sync = stat.BlockChainSynchronized
				set_chain_in_sync()
			}

			if (stat.WalletON != wallet_on) {
				wallet_on = stat.WalletON
				apply_wallet_on()
			}

			blno.title = "Last block received " + ((stat.Time_now-stat.Received)/60).toFixed(1) + " min ago"
			if (blno.innerText != stat.Height) {
				if (beep_new_block.checked)  {
					if (blno.innerText==BN_OFFLINE) {
						back_online_audio_file.play()
					} else if (blno.innerText.indexOf("...")==-1) {
						new_block_audio_file.play()
					}
				}
				new_block = true
				blno.innerText = stat.Height
				document.title = doc_title + ' @ ' + stat.Height
			}

			// dispatch event..
			var e = document.createEvent("Event")
			e.initEvent("lastblock", false, false)
			e.block = stat
			time_now = stat.Time_now
			blno.dispatchEvent(e)
		} catch(e) {
			blno.title = e
			blno.innerText = "???"
			//console.log()
		}
		setTimeout(refreshblock, new_block?1000:6000)
	}
	aj.open("GET","status.json",true)
	aj.send(null)
}

function set_chain_in_sync() {
	//console.log(chain_in_sync)
	chainsync_icon.style.display = chain_in_sync ? 'none' : 'inline'
}
set_chain_in_sync()

if (localStorage.getItem("new_block_beep")==="true") {
	beep_new_block.checked = true
}

document.addEventListener('DOMContentLoaded', function() {
	beep_new_block.onclick = function(event) {
		localStorage.setItem("new_block_beep", beep_new_block.checked?"true":"false")
		event.stopPropagation()
	}
	refreshblock()
})

</script>
